﻿using System;
using System.Web.UI;
using MTC.Web.Business.DTO;
using MTC.Common;
using MTC.Common.Enum;
using MTC.Web.Business;

namespace MTC.Web.Forms
{
    public partial class VehicleEdit : System.Web.UI.Page
    {
        private VehicleEntity _entity;
        Business.Vehicles VehiclesBO = new Business.Vehicles();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetupEnvironment();
                //get the driver id if available
                if (Request.Params[Params.VEHICLEID] != "0")
                {
                    int vehicleId = Convert.ToInt32(Request.Params[Params.VEHICLEID]);
                    _entity = VehiclesBO.GetAVehicle(vehicleId);
                    SetupEnvironment();
                    LoadFields();
                }
            }

        }

        private void LoadFields()
        {
            if (_entity == null)
                return;
            hfVehicleId.Value = _entity.VehicleId.ToString();

            cboBodyType.SelectedIndex = _entity.BodyType;
            txtEngineNo.Text = _entity.EngineNo;
            txtModel.Text = _entity.Model;
            txtPurchasePrice.Text = _entity.PurchasePrice.ToString();
            txtRego.Text = _entity.Rego;
            
            if (_entity.CarExpiryDate.HasValue)
                txtCarExpiryDate.Text = Utils.ChangeFormat(_entity.CarExpiryDate.Value);
            if (_entity.InspectionDate.HasValue)
                txtInspectionDate.Text = Utils.ChangeFormat(_entity.InspectionDate.Value);
            if (_entity.ManufactureDate.HasValue)
                txtManufactureDate.Text = Utils.ChangeFormat(_entity.ManufactureDate.Value);
            if (_entity.PurchaseDate.HasValue)
                txtPurchaseDate.Text = Utils.ChangeFormat(_entity.PurchaseDate.Value);
            if (_entity.RegistrationDueDate.HasValue)
                txtRegistrationDueDate.Text = Utils.ChangeFormat(_entity.RegistrationDueDate.Value);

        }

        private void SetupEnvironment()
        {
            cboBodyType.DataSource = BodyType.GetBodyTypes();
            cboBodyType.DataBind();

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ValidateFields())
            {
                SetFields();
            }


        }

        private bool SetFields()
        {
            VehicleEntity entity = new VehicleEntity();

            if (hfVehicleId.Value != "")
                entity.VehicleId = Convert.ToInt32(hfVehicleId.Value);
            entity.BodyType = cboBodyType.SelectedIndex;

            if (txtCarExpiryDate.Text.Length > 0)
                entity.CarExpiryDate = Convert.ToDateTime(txtCarExpiryDate.Text);

            entity.EngineNo = txtEngineNo.Text;
            if (txtInspectionDate.Text.Length > 0)
                entity.InspectionDate = Convert.ToDateTime(txtInspectionDate.Text);
            if (txtManufactureDate.Text.Length > 0)
                entity.ManufactureDate = Convert.ToDateTime(txtManufactureDate.Text);

            entity.Model = txtModel.Text;
            if (txtPurchaseDate.Text.Length > 0)
                entity.PurchaseDate = Convert.ToDateTime(txtPurchaseDate.Text);

            if (txtPurchasePrice.Text.Length > 0)
                entity.PurchasePrice = Convert.ToDecimal(txtPurchasePrice.Text);

            if (txtRegistrationDueDate.Text.Length > 0)
                entity.RegistrationDueDate = Convert.ToDateTime(txtRegistrationDueDate.Text);
            
            entity.Rego = txtRego.Text;

            if (entity.VehicleId == 0)
            {
                VehiclesBO.AddVehicle(entity);
            }
            else
            {
                return VehiclesBO.UpdateAVehicle(entity);
            }
            return true;

        }

        private bool ValidateFields()
        {
            return true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearFields();
            Response.Redirect(Common.Urls.VehicleListURL(), true);

        }

        private void ClearFields()
        {
            cboBodyType.ClearSelection();
            txtCarExpiryDate.Text = "";
            txtEngineNo.Text = "";
            txtInspectionDate.Text = "";
            txtManufactureDate.Text = "";
            txtModel.Text = "";
            txtPurchaseDate.Text = "";
            txtPurchasePrice.Text = "";
            txtRegistrationDueDate.Text = "";
            txtRego.Text = "";
            
        }
    }
}
